package com.gouwudai.gobbs.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IClassDAO;
import com.gouwudai.gobbs.pojo.TbbsClass;
import com.litt.core.cache.OSCacheManager;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.DBException;
import com.litt.core.util.html.EncodeString;

/**
 * 
 * <pre>
 *  Title:分类、版面DAO
 *  Description: 分类、版面DAO 
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-3-29
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class ClassDAOImpl extends BaseDAOImpl implements IClassDAO 
{
	private static final Logger logger = Logger.getLogger(ClassDAOImpl.class);	
	private static final String BUSI_NAME = "分类版面信息";	
	
	public void save(TbbsClass tbbsClass)
	{
		tbbsClass.setTotalTopic(0);
		tbbsClass.setTotalReply(0);
		tbbsClass.setTodayTopic(0);
		tbbsClass.setTodayReply(0);
		tbbsClass.setYesterTopic(0);
		tbbsClass.setYesterReply(0);		
		super.saveObject(tbbsClass);		
		OSCacheManager.removeCacheObject("CLASS_OPTIONS");
		OSCacheManager.removeCacheObject("ALL_CLASS_OPTIONS");
	}
	
	public TbbsClass load(Integer id)
	{
		return (TbbsClass)super.loadObject(TbbsClass.class, id);
	}   
    
    public void update(TbbsClass tbbsClass)
    {
        StringBuilder hql = new StringBuilder();
        hql.append("update TbbsClass set className=?");
        hql.append(",orders=?");
        hql.append(",parentId=?");
        hql.append(",classType=?");
        hql.append(",classStyle=?");
        hql.append(",hiddenFlag=?");
        hql.append(",childOpenFlag=?");
        hql.append(",classPro=?");
        hql.append(",accessGrade=?");
        hql.append(",topicDisNum=?");
        hql.append(",replyDisNum=?");
        hql.append(",mostOnline=?");
        hql.append(",summary=?");        
        hql.append(",upfileMaxSize=?");
        hql.append(",upfileOpenFlag=?");
        hql.append(" where classId=?");
        super.updateObject(hql.toString(), new Object[]{
        	tbbsClass.getClassName(), 
        	tbbsClass.getOrders(),
        	tbbsClass.getParentId(),
        	tbbsClass.getClassType(),
        	tbbsClass.getClassStyle(),
        	tbbsClass.getHiddenFlag(),
        	tbbsClass.getChildOpenFlag(),
        	tbbsClass.getClassPro(),
        	tbbsClass.getAccessGrade(),
        	tbbsClass.getTopicDisNum(),
        	tbbsClass.getReplyDisNum(),
        	tbbsClass.getMostOnline(),
        	tbbsClass.getSummary(),
        	tbbsClass.getUpfileMaxSize(),
        	tbbsClass.getUpfileOpenFlag(),
        	tbbsClass.getClassId()});
        OSCacheManager.removeCacheObject("CLASS_OPTIONS");
        OSCacheManager.removeCacheObject("ALL_CLASS_OPTIONS");
    }
    
    /**
     * 更新版面统计信息,同时更新父版面和父父版面的统计信息
     * @param classId
     * @throws DBException
     */
    public void updateTopicAdd(int classId,int parentId,int gradeId)
    {
    	int count = 1;
    	if(parentId>0)        
    		count++; 
    	if(gradeId>0)
    		count++; 
    	String[] hqls = new String[count];
    	hqls[0] = "update TbbsClass set totalTopic=totalTopic+1,todayTopic=todayTopic+1 where classId="+classId;
        if(parentId>0)
        {	
        	hqls[1] = "update TbbsClass set totalTopic=totalTopic+1,todayTopic=todayTopic+1 where classId="+parentId;
        	
        }	
        if(gradeId>0)
        {	
        	hqls[2] = "update TbbsClass set totalTopic=totalTopic+1,todayTopic=todayTopic+1 where classId="+gradeId;        	
        }
        
        super.executeHql(hqls);
        OSCacheManager.removeCacheObject("CLASS_OPTIONS");
        OSCacheManager.removeCacheObject("ALL_CLASS_OPTIONS");
    }    
    
    /**
     * 更新版面寄语
     * @param classId
     * @param summary
     * @throws DBException
     */
    public void updateSummary(Integer classId,String summary)
    {
        StringBuilder hql = new StringBuilder();
        hql.append("update TbbsClass set summary=?");
        hql.append(" where classId=?");
        super.updateObject(hql.toString(), new Object[]{EncodeString.htmlEncoder(summary),classId});
    }    

    /**
     * 删除当前分类/版面(包括子分类/版面)
     * @param classId 分类/版面ID
     */
    public void delete(Integer classId)
    {
        super.deleteObject(TbbsClass.class.getName(),"classId",classId); 
    }
    
    /**
     * 查找当前分类的子分类.
     * 
     * @param classId 分类/版面ID
     * 
     * @return List 
     */
    public List listSub(Integer classId)
    {
        String listHql = "from TbbsClass where parentId="+classId;
        return super.listAll(listHql);
    }
  
    /**
     * 条件查询父节点下指定类型的分类或版面(HQL查询).
     * 
     * @param parentId 父节点ID
     * @param hiddenFlag 是否隐藏标志
     * @param classType 类别：0 - 分类，1 - 版面
     * 
     * @return List
     */
    public List listClass(Integer parentId,Integer classType,Integer hiddenFlag)
    {
    	StringBuilder listHql = new StringBuilder("FROM TbbsClass WHERE 1=1");
    	if(classType!=null)
    		listHql.append(" AND classType="+classType);
    	if(hiddenFlag!=null)
    		listHql.append(" AND hiddenFlag="+hiddenFlag);
    	
    	if(parentId.intValue()>0)
    		listHql.append(" AND parentId="+parentId);
    	else
    		listHql.append(" AND (parentId IS NULL OR parentId=0)");    
    	listHql.append(" ORDER BY orders DESC");    	
    	return super.listAll(listHql.toString());    	 		
    }    
    
    /**
     * 条件查询父节点下指定类型的分类或版面(SQL查询).
     * 
     * @param parentId 父节点ID
     * @param hiddenFlag 是否隐藏标志
     * @param classType 类别：0 - 分类，1 - 版面
     * 
     * @return List
     */
    private List listClassType0(Integer parentId,Integer classType,Integer hiddenFlag)
    {
    	StringBuilder listSql = new StringBuilder("SELECT CLASS_ID,CLASS_NAME,CLASS_TYPE,CHILD_OPEN_FLAG FROM TbbsClass WHERE 1=1");
    	if(classType!=null)
    		listSql.append(" AND CLASS_TYPE="+classType);
    	if(hiddenFlag!=null)
    		listSql.append(" AND HIDDEN_FLAG="+hiddenFlag);
    	
    	if(parentId.intValue()>0)
    		listSql.append(" AND PARENT_ID="+parentId);
    	else
    		listSql.append(" AND (PARENT_ID IS NULL OR PARENT_ID=0)");    
    	listSql.append(" ORDER BY ORDERS DESC");    	
    	return super.listBySql(listSql.toString());    	  		
    }
    
    /**
     * 递归，按层次构造结果,现在只实现了4层
     * 类别只能是分类或版面的一种,构造下拉列表
     * @return
     */
    public List<Map> listClassByType(Integer classType,Integer hiddenFlag)
    {
    	List<Map> ret = new ArrayList<Map>();
    	
    	List<Map> rsList = this.listClassType0(0,classType,hiddenFlag);//递归查询所有结果					
		Map rsMap = null;
    	for(int i=0;i<rsList.size();i++)
    	{
    		rsMap = (Map)rsList.get(i);
    		ret.add(rsMap);    		
    		//第二层		    		
    		List<Map> rsList2 = this.listClassType0((Integer)rsMap.get("CLASS_ID"),classType,hiddenFlag);//递归查询所有结果					
			Map rsMap2 = null;
	    	for(int i2=0;i2<rsList2.size();i2++)
	    	{
	    		rsMap2 = (Map)rsList2.get(i2);
	    		rsMap2.put("CLASS_NAME", "&nbsp;&nbsp;&nbsp;&nbsp;"+(String)rsMap2.get("CLASS_NAME"));
	    		ret.add(rsMap2);    		
	    		//第三层			    		
	    		List<Map> rsList3 = this.listClassType0((Integer)rsMap2.get("CLASS_ID"),classType,hiddenFlag);//递归查询所有结果					
				Map rsMap3 = null;
		    	for(int i3=0;i3<rsList3.size();i3++)
		    	{
		    		rsMap3 = (Map)rsList3.get(i3);
		    		rsMap3.put("CLASS_NAME", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+(String)rsMap3.get("CLASS_NAME"));
		    		ret.add(rsMap3); 
		    		//第四层				    		
		    		List<Map> rsList4 = this.listClassType0((Integer)rsMap3.get("CLASS_ID"),classType,hiddenFlag);//递归查询所有结果					
					Map rsMap4 = null;
			    	for(int i4=0;i4<rsList4.size();i4++)
			    	{
			    		rsMap4 = (Map)rsList4.get(i4);
			    		rsMap4.put("CLASS_NAME", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+(String)rsMap4.get("CLASS_NAME"));
			    		ret.add(rsMap4); 
			    	}
		    	}
	    	}
    	}
    	return ret;
    }
    
    /**
     * 分页查询
     * @param pageParam 分页查询对象
     * @return IPageList
     */
    public IPageList listPage(PageParam pageParam) 
    {       
    	Integer parentId = Utility.parseInt(pageParam.getStringCond("parentId"));
    	
    	StringBuilder listSql = new StringBuilder("SELECT * FROM TbbsClass WHERE 1=1");
    	if(parentId.intValue()>0)
    		listSql.append(" AND PARENT_ID="+parentId);
    	else
    		listSql.append(" AND (PARENT_ID IS NULL OR PARENT_ID=0) ");
    	if(!Utility.isEmpty(pageParam.getSort()))
    	    listSql.append(pageParam.getSort());
    	return super.listSqlPage(listSql.toString(),pageParam.getPageIndex(),pageParam.getPageSize());
    }
    

}
